--[[
	掉落奖励
]]
local filename = "dropreward.lua"

local timetool = require "timetool"
local skynet = require "skynet"
local cluster = require "skynet.cluster"
local filelog =require "filelog"
local redisdao = require "redisdao"
local base = require "base"
require "pubenum"

local json = require "cjson"
json.encode_sparse_array(true,1,1)


local service = nil
local CMD = {
	service = nil
}

--小游戏列表
-- dds 打地鼠 dfj 打飞机
local gamelist = { dds = true, dfj = false}

--默认掉落奖励控制表
local Default_ctrl_list = {
    {
        prop_id = 2,    --道具id：金豆
        value = 100,    --奖励数量
        prob =  0.1,    --掉落概率
        limit = 1000,   --掉落上限
    },
}

-----------------------------------

--获得掉落奖励控制表
local function get_dropreward_ctrl_list()
    local ctrl_list = nil
    local result,resut_data = redisdao.query_data(".gmmqredis", "get", "dropreward_ctrl_list")
    if result and resut_data then
        ctrl_list = json.decode(resut_data)
    end
    return ctrl_list or Default_ctrl_list
end

--根据权值表，获取随机结果
local function get_random_result_by_weightlist(weight_list, total_w)
    local r = base.get_random(1,total_w)
    local sum = 0
    for id,w in pairs(weight_list) do
        sum = sum + w
        if r <= sum then
            return id
        end
    end
    return   
end

--检测是否小于掉落奖励上限
local function check_is_lesslimit(rid, rewarditem)
    local dropreward_record = nil

    local result,resut_data = redisdao.query_data(".gmmqredis", "get", rid .. ":dropreward_record")
    if result and resut_data then
        dropreward_record = json.decode(resut_data)
    end

    --不是同一天，重置记录
    dropreward_record = dropreward_record or {}
    local curtime = timetool.get_time()
    local curday = os.date("%d", curtime)
    if dropreward_record.record_time then
        local lastday = os.date("%d",tonumber(dropreward_record.record_time))
        if lastday ~= curday then
            dropreward_record = {}
        end
    end

    --检测上限
    local num = tonumber(rewarditem.value) or 0
    local totalnum = dropreward_record[tostring(rewarditem.prop_id)] or 0
    num = num + (tonumber(totalnum) or 0)
    if num > rewarditem.limit then
        return false
    end

    --更新记录
    dropreward_record.record_time = curtime
    dropreward_record[tostring(rewarditem.prop_id)] = num
    redisdao.query_data(".gmmqredis", "set", rid .. ":dropreward_record", json.encode(dropreward_record))

    return true
end

--获得掉落奖励
local function get_dropreward(rid)
	local rewards = { }
    local ctrl_list = get_dropreward_ctrl_list()

    local w_list = {}
    for k,item in pairs(ctrl_list) do
        w_list[k] = math.floor(item.prob * 1000)
    end
    local key = get_random_result_by_weightlist(w_list,1000)
    if not key then
        return rewards
    end

    --检测是否超获取上限
    local rewarditem = ctrl_list[key]
    if rewarditem and check_is_lesslimit(rid,rewarditem) then
	    table.insert(rewards,{prop_id = rewarditem.prop_id, value = rewarditem.value})
    end
	
	return rewards
end

--
--小游戏中触发奖励
--
function CMD.process(rid, msgbody)
	if not service then
		service = CMD.service
	end

    if not msgbody or not gamelist[msgbody.gamename] then
        return {retcode = EErrCode.ERR_INVALID_PARAMS, retstr = "无效的参数"}
    end
	
	local setargs = nil
    local is_coin = false
	local is_diamond = false
	local rewards = get_dropreward(rid)
	local beginvalue = service.money.diamond
	for _,item in pairs(rewards) do
		local value = item.value
		local prop_id = item.prop_id
		if prop_id == 1 then
					
		elseif prop_id == 2 then
			is_diamond = value
			setargs = service:change_money({diamond = value})
		elseif prop_id == 3 then
            is_coin = value
			setargs = service:change_money({coin = value})
		elseif prop_id then
			service:charge_prop(prop_id, value,{})
		end
	end
   
    --没有扣金豆或金币，直接返回
	if not is_diamond and not is_coin then
		return {gamename = msgbody.gamename, reward_list = json.encode(rewards)}
	end
	
    local value = is_diamond or is_coin
    if setargs then
        --记录日志
        local logstr = nil
        local comment = { }
        local now = timetool.get_time()
        comment.tradeid = skynet.call(".nodeuuid", "lua", "getuuid")
        comment.table_uuid = msgbody.gamename .. "node" .. "_" .. 0 .. "_" .. 0 .. "_" .. now
        if is_diamond then
            logstr = string.format("%d,%d,%d,%d,%d,%d,%d,%d,%d,%s,%s,%s", EDiamondChange.SMALLGAME_REWARD, rid, service.info.level, value, 0, 0, beginvalue, service.money.diamond, service.money.lock_diamond, comment.table_uuid, now, comment.tradeid)
            skynet.send(".diamondlog", "lua", "write_file_redis", logstr, rid)
        else
            logstr = string.format("%d,%d,%d,%d,%d,%d,%d,%d,%d,%s,%s,%s", ECoinChange.SMALLGAME_REWARD, rid, service.info.level, value, 0, 0, beginvalue, service.money.coin, service.money.lock_coin, comment.table_uuid, now, comment.tradeid)
            skynet.send(".coinlog", "lua", "write", logstr)
        end
        return {gamename = msgbody.gamename, reward_list = json.encode(rewards)}
    end

    return {retcode = EErrCode.ERR_INVALID_REQUEST, retstr = "获取奖励失败"}
end

return CMD
